//
//  MyGroupViewController.swift
//  HoneyBee
//
//  Created by point on 2018/1/24.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import MJRefresh

private let kCellID = "kTopicGroupCellId"

class MyGroupViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    
    var pageNo : Int = 1
    var pageSize: Int = 10
    
    private var groupList:[TopicGroupModel] = [TopicGroupModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    public func loadData(){
        
        self.hudLoad()
        
        let dict = [
            "userId":String(UserInfo.shareInstance.userModel().id),
            "pageNo":self.pageNo,
            "pageSize":self.pageSize] as [String : Any]
        
        TempNetTool.post( kHost + kqueryJoinedGroupList + "".apiF(time: "1", sign: "dd") + "&pageNo=\(self.pageNo)" + "&pageSize=\(self.pageSize)", parameters: dict, progress: nil, success: { [weak self] (result) in
            self?.tableView.mj_header.endRefreshing()
            self?.tableView.mj_footer.endRefreshing()
            if (self?.pageNo == 1) {
                self?.groupList.removeAll()
            }
            if (result != nil) {
                guard let resultDict = result as? [String : NSObject] else { return }
                guard let dataArray = resultDict["data"] as? [NSObject] else { return }
                let groupModels = TopicGroupModel.tmSpeelBreakModelArr(dataArray)
                if (groupModels.count < (self?.pageSize)!){
                    self?.tableView.mj_footer.endRefreshingWithNoMoreData()
                }
                self?.groupList.append(contentsOf: (groupModels as? [TopicGroupModel])!)
                
                self?.tableView.reloadData()
            }
            self?.hudHide()
            
            
        }) {[weak self]   (error) in
            self?.hudHide()
            self?.tableView.mj_header.endRefreshing()
            self?.tableView.mj_footer.endRefreshing()
            
        }
        
    }

}
extension MyGroupViewController:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.groupList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath) as! TopicMyGroupTableViewCell
        cell.configModel(model: self.groupList[indexPath.item], index: indexPath)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: false)
        
        let model = self.groupList[indexPath.item]
        let vc = GroupTopicViewController()
        vc.configModel(model: model);
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    private func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    
}


extension MyGroupViewController {
    fileprivate func setupUI(){
        
        tableView.register(UINib(nibName: "TopicMyGroupTableViewCell", bundle: nil), forCellReuseIdentifier: kCellID)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.backgroundColor = UIColor(hex: "#eeeff0")

        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.pageNo = 1
            self.loadData()
        })
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {
            self.pageNo += 1
            self.loadData()
        })
        tableView.mj_header.beginRefreshing()
        
    }
    
}
